home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / X11R4 / cmds / X / ddx / sprite.X11R3 / spriteMouse.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-10-08  |  7.7 KB  |  287 lines

  1. /*-
  2.  * spriteMouse.c --
  3.  *    Functions for playing cat and mouse... sorry.
  4.  *
  5.  * Copyright (c) 1987, 1989 by the Regents of the University of California
  6.  *
  7.  * Permission to use, copy, modify, and distribute this
  8.  * software and its documentation for any purpose and without
  9.  * fee is hereby granted, provided that the above copyright
  10.  * notice appear in all copies.  The University of California
  11.  * makes no representations about the suitability of this
  12.  * software for any purpose.  It is provided "as is" without
  13.  * express or implied warranty.
  14.  *
  15.  *
  16.  */
  17. #ifndef lint
  18. static char rcsid[] =
  19.     "$Header: /a/X/src/cmds/Xsprite/ddx/sun3.md/RCS/spriteMouse.c,v 1.2 87/06/20 19:56:48 deboor Exp Locker: ouster $ SPRITE (Berkeley)";
  20. #endif lint
  21.  
  22. #define NEED_EVENTS
  23. #include    "spriteddx.h"
  24.  
  25. typedef struct {
  26.     int        bmask;        /* Current button state */
  27.     Bool    mouseMoved;        /* Mouse has moved */
  28. } SunMsPrivRec, *SunMsPrivPtr;
  29.  
  30. static void           spriteMouseCtrl();
  31. static int           spriteMouseGetMotionEvents();
  32. static void           spriteMouseProcessEvent();
  33. static void           spriteMouseDoneEvents();
  34.  
  35. /*
  36.  * Because lastEventTime is actual time, while event times are since-boot
  37.  * times, we keep the time of the most recent mouse event for
  38.  * the DoneEvents procedure to use.
  39.  */
  40. static int            lastMouseEventTime = 0;
  41.  
  42. static SunMsPrivRec    spriteMousePriv;
  43. PtrPrivRec     sysMousePriv = {
  44.     -1,                /* Descriptor to device */
  45.     (Mouse_Event *(*)())NoopDDA,/* Function to read events -- done by
  46.                  * keyboard */
  47.     spriteMouseProcessEvent,    /* Function to process an event */
  48.     spriteMouseDoneEvents,    /* When all the events have been */
  49.                 /* handled, this function will be */
  50.                 /* called. */
  51.     0,                /* Current X coordinate of pointer */
  52.     0,                /* Current Y coordinate */
  53.     NULL,            /* Screen pointer is on */
  54.     (pointer)&spriteMousePriv,    /* Field private to device */
  55. };
  56.  
  57. /*-
  58.  *-----------------------------------------------------------------------
  59.  * spriteMouseProc --
  60.  *    Handle the initialization, etc. of a mouse
  61.  *
  62.  * Results:
  63.  *    none.
  64.  *
  65.  * Side Effects:
  66.  *
  67.  *-----------------------------------------------------------------------
  68.  */
  69. int
  70. spriteMouseProc (pMouse, what)
  71.     DevicePtr      pMouse;       /* Mouse to play with */
  72.     int              what;            /* What to do with it */
  73. {
  74.     BYTE          map[4];
  75.  
  76.     switch (what) {
  77.     case DEVICE_INIT:
  78.         if (pMouse != LookupPointerDevice()) {
  79.         ErrorF ("Cannot open non-system mouse");    
  80.         return (!Success);
  81.         }
  82.         
  83.         sysMousePriv.pScreen = &screenInfo.screen[0];
  84.         sysMousePriv.x = sysMousePriv.pScreen->width / 2;
  85.         sysMousePriv.y = sysMousePriv.pScreen->height / 2;
  86.  
  87.         spriteMousePriv.bmask = 0x87;   /* All buttons up */
  88.         spriteMousePriv.mouseMoved = FALSE;
  89.  
  90.         pMouse->devicePrivate = (pointer) &sysMousePriv;
  91.         pMouse->on = FALSE;
  92.         map[1] = 1;
  93.         map[2] = 2;
  94.         map[3] = 3;
  95.         InitPointerDeviceStruct(
  96.         pMouse, map, 3, spriteMouseGetMotionEvents, spriteMouseCtrl);
  97.         break;
  98.     case DEVICE_ON:
  99.         break;
  100.     case DEVICE_CLOSE:
  101.     case DEVICE_OFF:
  102.         break;
  103.     }
  104.     return (Success);
  105. }
  106.         
  107. /*-
  108.  *-----------------------------------------------------------------------
  109.  * spriteMouseCtrl --
  110.  *    Alter the control parameters for the mouse. Since acceleration
  111.  *    etc. is done from the PtrCtrl record in the mouse's device record,
  112.  *    there's nothing to do here.
  113.  *
  114.  * Results:
  115.  *    None.
  116.  *
  117.  * Side Effects:
  118.  *    None.
  119.  *
  120.  *-----------------------------------------------------------------------
  121.  */
  122. static void
  123. spriteMouseCtrl (pMouse)
  124.     DevicePtr      pMouse;
  125. {
  126. }
  127.  
  128. /*-
  129.  *-----------------------------------------------------------------------
  130.  * spriteMouseGetMotionEvents --
  131.  *    Return the (number of) motion events in the "motion history
  132.  *    buffer" (snicker) between the given times.
  133.  *
  134.  * Results:
  135.  *    The number of events stuffed.
  136.  *
  137.  * Side Effects:
  138.  *    The relevant xTimecoord's are stuffed in the passed memory.
  139.  *
  140.  *-----------------------------------------------------------------------
  141.  */
  142. static int
  143. spriteMouseGetMotionEvents (buff, start, stop)
  144.     CARD32 start, stop;
  145.     xTimecoord *buff;
  146. {
  147.     return 0;
  148. }
  149.  
  150. /*-
  151.  *-----------------------------------------------------------------------
  152.  * MouseAccelerate --
  153.  *    Given a delta and a mouse, return the acceleration of the delta.
  154.  *
  155.  * Results:
  156.  *    The corrected delta
  157.  *
  158.  * Side Effects:
  159.  *    None.
  160.  *
  161.  *-----------------------------------------------------------------------
  162.  */
  163. static short
  164. MouseAccelerate (pMouse, delta)
  165.     DevicePtr      pMouse;
  166.     int              delta;
  167. {
  168.     register int  sgn = sign(delta);
  169.     register PtrCtrl *pCtrl;
  170.  
  171.     delta = abs(delta);
  172.     pCtrl = &((DeviceIntPtr) pMouse)->u.ptr.ctrl;
  173.  
  174.     if (delta > pCtrl->threshold) {
  175.     return ((short) (sgn * (pCtrl->threshold +
  176.                 ((delta - pCtrl->threshold) * pCtrl->num) /
  177.                 pCtrl->den)));
  178.     } else {
  179.     return ((short) (sgn * delta));
  180.     }
  181. }
  182.  
  183. /*-
  184.  *-----------------------------------------------------------------------
  185.  * spriteMouseProcessEvent --
  186.  *    Given a Firm_event for a mouse, pass it off the the dix layer
  187.  *    properly converted...
  188.  *
  189.  * Results:
  190.  *    None.
  191.  *
  192.  * Side Effects:
  193.  *    The cursor may be redrawn...? devPrivate/x/y will be altered.
  194.  *
  195.  *-----------------------------------------------------------------------
  196.  */
  197. static void
  198. spriteMouseProcessEvent (pMouse, ev)
  199.     DevicePtr      pMouse;       /* Mouse from which the event came */
  200.     Mouse_Event  *ev;            /* Event to process */
  201. {
  202.     xEvent        xE;
  203.     register PtrPrivPtr    pPriv;    /* Private data for pointer */
  204.     register SunMsPrivPtr pSunPriv; /* Private data for mouse */
  205.     register int      bmask;    /* Temporary button mask */
  206.     register int      button;
  207.  
  208.     pPriv = (PtrPrivPtr)pMouse->devicePrivate;
  209.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  210.  
  211.     xE.u.keyButtonPointer.time = ev->time;
  212.  
  213.     bmask = ev->key ^ pSunPriv->bmask;
  214.  
  215.     if (ev->deltaX) {
  216.     pPriv->x += MouseAccelerate (pMouse, ev->deltaX);
  217.     if (spriteConstrainXY (&pPriv->x, &pPriv->y)) {
  218.         pSunPriv->mouseMoved = TRUE;
  219.     }
  220.     }
  221.  
  222.     if (ev->deltaY) {
  223.     pPriv->y -= MouseAccelerate (pMouse, ev->deltaY);
  224.     if (spriteConstrainXY (&pPriv->x, &pPriv->y)) {
  225.         pSunPriv->mouseMoved = TRUE;
  226.     }
  227.     }
  228.     
  229.     if (ev->key ^ pSunPriv->bmask) {
  230.     spriteMouseDoneEvents (pMouse, FALSE);
  231.     }
  232.     xE.u.keyButtonPointer.rootX = pPriv->x;
  233.     xE.u.keyButtonPointer.rootY = pPriv->y;
  234.     xE.u.keyButtonPointer.time = ev->time;
  235.  
  236.     for (bmask = 4, button = 1; bmask != 0; bmask >>= 1, button++) {
  237.     if ((ev->key & bmask) != (pSunPriv->bmask & bmask)) {
  238.         xE.u.u.type = (ev->key & bmask) ? ButtonRelease : ButtonPress;
  239.         xE.u.u.detail = button;
  240.  
  241.         (* pMouse->processInputProc) (&xE, pMouse);
  242.     }
  243.     }
  244.     pSunPriv->bmask = ev->key;
  245.     lastMouseEventTime = ev->time;
  246. }
  247.  
  248. /*-
  249.  *-----------------------------------------------------------------------
  250.  * spriteMouseDoneEvents --
  251.  *    Finish off any mouse motions we haven't done yet. (At the moment
  252.  *    this code is unused since we never save mouse motions as I'm
  253.  *    unsure of the effect of getting a keystroke at a given [x,y] w/o
  254.  *    having gotten a motion event to that [x,y])
  255.  *
  256.  * Results:
  257.  *    None.
  258.  *
  259.  * Side Effects:
  260.  *    A MotionNotify event may be generated.
  261.  *
  262.  *-----------------------------------------------------------------------
  263.  */
  264. /*ARGSUSED*/
  265. static void
  266. spriteMouseDoneEvents (pMouse, final)
  267.     DevicePtr      pMouse;
  268.     Bool      final;
  269. {
  270.     PtrPrivPtr      pPriv;
  271.     SunMsPrivPtr  pSunPriv;
  272.     xEvent      xE;
  273.  
  274.     pPriv = (PtrPrivPtr) pMouse->devicePrivate;
  275.     pSunPriv = (SunMsPrivPtr) pPriv->devPrivate;
  276.  
  277.     if (pSunPriv->mouseMoved) {
  278.     spriteMoveCursor (pPriv->pScreen, pPriv->x, pPriv->y);
  279.     xE.u.keyButtonPointer.rootX = pPriv->x;
  280.     xE.u.keyButtonPointer.rootY = pPriv->y;
  281.     xE.u.keyButtonPointer.time = lastMouseEventTime;
  282.     xE.u.u.type = MotionNotify;
  283.     (* pMouse->processInputProc) (&xE, pMouse);
  284.     pSunPriv->mouseMoved = FALSE;
  285.     }
  286. }
  287.